package com.biscuit.page.basicservice.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.Random;
import java.util.UUID;

/**
 * @author mazihao
 * @createTime 2024/3/23 14:53
 */
public class CommonUtil {

    public static String getIconUrlByUrl(String url) {
        try {
            Document doc = Jsoup.connect(url).get();
            Element iconTag = doc.select("link[rel='icon']").first();
            if (iconTag != null) {
                return iconTag.attr("href"); // 获取图标地址
            } else {
                return "";
            }
        } catch (Exception e) {
            return "";
        }
    }

    public static String generateVerificationCode(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < length; i++) {
            // 生成 0 到 9 之间的随机数字并添加到 StringBuilder 中
            sb.append(random.nextInt(10));
        }

        return sb.toString();
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

}
